package com.group.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.dao.TbAttrNameMapper;
import com.group.pojo.Common;
import com.group.pojo.TbAttrName;
import com.group.pojo.TbAttrNameExample;
import com.group.pojo.TbAttrNameExample.Criteria;
import com.group.service.AttrNameService;
import com.group.service.CategoryService;
import com.group.service.CommonService;
import com.group.util.CommonUtil;
@Service
public class AttrNameServiceImpl implements AttrNameService {
	@Autowired
	private TbAttrNameMapper attrNameMapper ;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private CommonService commonService;
	
	private void setCondition(Criteria criteria, TbAttrName attrName) {
		String id = attrName.getId();
		if (id != null && !"".equals(id)) {
			criteria.andIdEqualTo(id);
		}
		
		String attrNameName = attrName.getAttrName();
		if (attrNameName != null && !"".equals(attrNameName)) {
			criteria.andAttrNameEqualTo(attrNameName);
		}
		
		String attrNameStatus = attrName.getAttrNameStatus();
		if (attrNameStatus != null && !"".equals(attrNameStatus)) {
			criteria.andAttrNameStatusEqualTo(attrNameStatus);
		}
		
		String userId = attrName.getCreateUserId();
		if (userId != null && !"".equals(userId)) {
			criteria.andCreateUserIdEqualTo(userId);
		}
	}
	
	@Override
	public List<TbAttrName> query(TbAttrName attrName) {
		List<TbAttrName> list = attrNameMapper.query(attrName);
		return list;
	}
	//删除属性
	@Override
	public Map<String, String> batchDelete(String[] attrIds) {
		TbAttrName attr = new TbAttrName();
		attr.setAttrNameStatus("0");
		Map<String, String> map = CommonUtil.getReturnMap("success", "true");
		for (String attrId : attrIds) {
			attr.setId(attrId);
			int update = attrNameMapper.updateByPrimaryKeySelective(attr);
			if(update != 1){
				map.put("success", "false");
				return map;
			}
		}
		return map;
	}
	 
	//根据属性id查询属性信息
	@Override
	public List<TbAttrName> getAttrById(String id) {
		TbAttrNameExample example = new TbAttrNameExample();
		example.createCriteria().andIdEqualTo(id);
		return attrNameMapper.selectByExample(example);
	}
	//修改属性
	@Override
	public Map<String, String> update(TbAttrName attrName) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		int update = attrNameMapper.updateByPrimaryKeySelective(attrName);
		if(update == 1){
			returnMap.put("success", "true");
		}
		return returnMap;
	}
	//根据属性id获得属性名称
	@Override
	public String getAttrNameById(String attrId) {
        TbAttrNameExample example = new TbAttrNameExample();
        example.createCriteria().andIdEqualTo(attrId);
        String attrName = attrNameMapper.selectByExample(example).get(0).getAttrName();
		return attrName;
	}
	//根据属性名称获得属性id
	@Override
	public String getAttrNameIdByName(String name) {
		TbAttrNameExample example = new TbAttrNameExample();
		example.createCriteria().andAttrNameEqualTo(name).andAttrNameStatusEqualTo("1");
		List<TbAttrName> list = attrNameMapper.selectByExample(example);
		if(list.size()==0){
			return null;
		}else{
		  String attrNameId = list.get(0).getId();
		return attrNameId;
		}
	}
	//检查属性名称是够存在
	@Override
	public String CheckattrName(String attrName) {
		TbAttrNameExample example = new TbAttrNameExample();
		example.createCriteria().andAttrNameEqualTo(attrName).andAttrNameStatusEqualTo("1");
		int cunt = attrNameMapper.countByExample(example);
		if(cunt!=0){
			return "false";
		}
		return "true";
	}
	// 修改时检查属性名称
	@Override
	public String updateCheckattrName(String attrName,String id) {
		TbAttrNameExample example = new TbAttrNameExample();
		TbAttrNameExample example1 = new TbAttrNameExample();
		example.createCriteria().andAttrNameEqualTo(attrName).andAttrNameStatusEqualTo("1");
		example1.createCriteria().andIdEqualTo(id).andAttrNameStatusEqualTo("1");
		int cunt = attrNameMapper.countByExample(example);
		if (cunt!=0){
			List<TbAttrName> list = attrNameMapper.selectByExample(example);
			List<TbAttrName> list1 = attrNameMapper.selectByExample(example1);
			if(list.get(0).getAttrName().equals(list1.get(0).getAttrName())){
				return "true";
			}else{
				return "false";
			}
		}else{
			return "true";
		}
	}

}
